package com.gzsxy.esjy.service.netty.fileServer.schedule;


import com.gzsxy.esjy.service.netty.service.FileUploadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;



/**
 * 计划删除文件
 * @author zhangxiaolong
 * @date 2022/7/11 9:36
 */
@EnableScheduling
@Component
public class DeleteFileSechedle {
	
	private final static Logger log = LoggerFactory.getLogger(DeleteFileSechedle.class) ;

	private final  static DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Autowired
	private FileUploadService fileUploadService;

	private volatile  boolean isRun  = false;

	/**
	 * 定时清除过期文件
	 */
	@Scheduled(cron = "0 0 1 * * ?")
    private void deleteFiles() {
		log.info("定时清除过期文件开始,当前时间【】",DATE_FORMAT.format(new Date()));
		if (!isRun){
			isRun = true;
			try {
				fileUploadService.deleteFiles();
			} catch (Exception e) {
				log.error("定时删除文件异常");
			}
			isRun = false;
		}


	}
}
